﻿using System;
using System.Data;
using VIT.Web.UI;
using VIT.Web.Controls;
using VIT.Provider;
using System.Web.UI.WebControls;

namespace VIT.Web.Admin
{
    public partial class PageList : VITComponent
    {
        VIT.Provider.Page obj;
        protected void Page_Load(object sender, EventArgs e)
        {
            obj = new VIT.Provider.Page();

            if (!Page.IsPostBack)
                loadLang();
        }

        #region Methods
        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select)]
        public DataTable GetData(string SortColumn)
        {
            VIT.Provider.Page p = new VIT.Provider.Page();
            p.LanguageId = Session["language"].ToString();
            return p.GetList(SortColumn);
        }

        private void loadLang()
        {
            ddlLang.DataSource = new Language().getAll();
            ddlLang.DataTextField = "Name";
            ddlLang.DataValueField = "Id";
            ddlLang.DataBind();

            ddlLang.SelectedValue = Session["language"].ToString();
        }

        public void deleteList(string[] listId)
        {
            foreach (string id in listId)
            {
                obj.Id = id;
                obj.LanguageId = Session["language"].ToString();
                obj.GetSingle();
                if (obj.Delete()) msgSuccess.Visible = true;
                else msgError.Visible = true;
            }
        }
        #endregion

        #region Events
        protected void Delete_Click(object sender, EventArgs e)
        {
            string[] check = Request["itemCheckbox"].Split(',');
            deleteList(check);
            ListView.DataBind();
        }

        protected void Insert_Click(object sender, EventArgs e)
        {
            RedirectComponent("Page", "PageEdit");
        }

        protected void ddlLang_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["language"] = ddlLang.SelectedValue;
            ListView.DataBind();
        }
        #endregion
    }
}
